public class Test3 {

    public synchronized void fun1(){
            System.out.println("A线程上锁");
            System.out.println("A线程开始使用资源");
            try {
                System.out.println("A线程调用wait,阻塞状态");
                this.wait();
                System.out.println("B线程释放锁之后，A线程重新加锁");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    public synchronized void fun2(){
            System.out.println("B线程上锁");
            System.out.println("B线程开始使用资源");
            System.out.println("B线程调用notify唤醒A线程");
            this.notify();
            System.out.println("B线程继续执行");
            System.out.println("全部执行完之后B线程解锁");
        }

    public static void main(String[] args) throws InterruptedException {
        Test3 test=new Test3();
        Thread thread=new Thread(){
            @Override
            public void run() {
                test.fun1();
            }
        };

        Thread thread1=new Thread(){
            @Override
            public void run() {
                test.fun2();
            }
        };
        thread.start();
        thread1.start();
        thread.join();
        thread.join();

    }
    public static void main1(String[] args) {
        Thread thread=new Thread(()->{
            while (!Thread.currentThread().isInterrupted()) {
                System.out.println("hello Thread");
                try {
                    //刚开始中断信号 interrupt为false
                   Thread.sleep(1000);
                    //B线程是否愿意退出线程？
                } catch (InterruptedException e) {
                    //继续执行
                    ;
                }
            }
        });
        thread.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        thread.interrupt();

    }
}
